package org.goyo.main.domain.dao.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.exception.DBException;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.BaseDAO;
import org.goyo.main.domain.dao.system.ISysRoleDao;
import org.goyo.main.domain.entity.system.SysRole;
import org.springframework.stereotype.Repository;
@Repository
public class SysRoleDaoImpl extends BaseDAO implements ISysRoleDao {
	
	@Override
	public Pager findByCond(String cond, Pager pager) {
		pager.setSortname("roleNum");
		StringBuffer buf = new StringBuffer("from SysRole where 1=1 ");
		if (StringUtils.isNotBlank(cond)) {
          buf.append(" and ");
          buf.append(cond);
        }
            String hql = this.addSort(buf, pager);
            String countHql = "select count(*) "+buf.toString();
         return this.findObjectsByPager(hql,countHql, pager);
	}

	
	public List<Object> getCondition(StringBuffer buf,Map<String, Object> cond){
		List<Object> params = new ArrayList<Object>();
		if(cond.get("roleName") != null && !"".equals(cond.get("roleName"))){
			buf.append(" and roleName= ? ");
			params.add(cond.get("roleName"));
		}
		return params;
	}

	@Override
	public int findByCondCount(Map<String, Object> cond) {
		StringBuffer buf = new StringBuffer("select count(*) from SysRole where 1=1 ");
		List<Object> params = getCondition(buf, cond);
		int total = this.getTotalByCond(buf.toString(), params.toArray());
		return total;
	}

	@Override
	public List<SysRole> findByUserId(String userId) throws DBException {
		String hql = "from SysRole where userId=?";
		return this.findObjects(hql, null, new Object[]{userId});
	}
}
